<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\cms\api\controller;

use think\exception\ValidateException;
use app\api\BaseController;
use plugins\cms\api\validate\News as Validate;
use plugins\cms\api\model\News;
use plugins\cms\api\model\NewsComment;
/**
 * 我的新闻评论
 */
class UserNewsComment extends BaseController
{
    /**
     * 用户权鉴
     */
    public $middleware = [\app\api\middleware\AuthCheck::class];
    
    /**
     * 显示资源列表
     */ 
    public function index()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('userCommentIndex')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $where[] = ['user_id', '=', $this->request->userInfo->id];
            $search = ['keyword', 'date'];
            $order  = ['create_time' => 'desc'];
            $count = NewsComment::where($where)->withSearch($search, $input)->count();
            $data  = NewsComment::where($where)->withSearch($search, $input)->with(['news'])->append(['like'])->order($order)->page($input['page'], 10)->select();
            return json(['status' => 'success', 'message' => '获取成功', 'data' => $data, 'count' => $count]);
        }
    }
    
    /**
     * 保存新建的资源
     */
    public function save()
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('userCommentSave')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            $last = NewsComment::where('user_id', $this->request->userInfo->id)->order('create_time', 'desc')->value('create_time');
            if ($last) {
                if ((time() - strtotime($last)) < 2) {
                    return json(['status' => 'error', 'message' => '违规操作']);
                }
            }
            $news = News::where('id',$input['id'])->find();
            $data = NewsComment::create([
                'pid'           => $input['pid'],
                'user_id'       => $this->request->userInfo->id,
                'reply_user_id' => $input['reply_user_id'],
                'news_id'       => $news->id,
                'news_cover'    => $news->cover,
                'news_title'    => $news->title,
                'content'       => $input['content'],
                'like'          => [],
                'create_time'   => date('Y-m-d H:i:s'),
            ]);
            return json(['status' => 'success', 'message' => '评论成功', 'data' => $data]);
        }
    }
    
    /**
     * 删除指定资源
     */
    public function delete() 
    {
        if ($this->request->isPost()) {
            try {
                $input = input('post.');
                validate(Validate::class)->scene('delete')->check($input);
            } catch ( ValidateException $e ) {
                return json(['status' => 'error', 'message' => $e->getError()]);
            }
            NewsComment::where('id',$input['id'])->where('user_id', $this->request->userInfo->id)->delete();
            return json(['status' => 'success', 'message' => '删除成功']);
        }
    }
}